﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControlsLib;
using GestionePolveriLib.BL.Reports;
using GestionePolveriLib.Facades.Reports;
using GestionePolveri2.DAL;
using GestionePolveri2.Filters;
using GestionePolveriLib.BL.FilterBags;
using Microsoft.Reporting.WinForms;
using FrameworkLib.Utilities.Export;

namespace GestionePolveri2.Reports
{
    public partial class ReportDirezionaleDettaglioPolveriXXXX : ReportDirezionaleDettaglioPolveriXXXXBase
    {
        ReportDirezionaleFilter m_filter = null;

        public ReportDirezionaleDettaglioPolveriXXXX()
        {
            InitializeComponent();
        }

        public override void InitializeForm()
        {
            base.InitializeForm();

            if (!DesignMode)
            {
                this.EnableSecurityOnData = true;

                this.AddCustomButton("btnEsportaToExcel", "Esporta in Excel");
                this.CustomButtonClicked += new EventHandler<CustomEventArgs<ReportDirezionaleFilterBag>>(ReportDirezionale_CustomButtonClicked);

                m_filter = new ReportDirezionaleFilter();
                m_filter.UseDatesFilter = false;
                m_filter.ExecuteFilter += new EventHandler(cf2_ExecuteFilter);
                this.Filter = m_filter;

                this.UseFilter = true;
            }
        }

        void ReportDirezionale_CustomButtonClicked(object sender, CustomEventArgs<ReportDirezionaleFilterBag> e)
        {
            if (e.ButtonName.Equals("btnEsportaToExcel", StringComparison.InvariantCultureIgnoreCase))
            {
                ReportDirezionaleFacade rdf = new ReportDirezionaleFacade(m_filter.FilterBag);
                rdf.RunAsync(() => rdf.GetReportDataDettaglioXXXX(), (result) =>
                {
                    if (result != null)
                    {
                        //string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                        //string filename = string.Format(@"{0}\Prova.xls", path);

                        SaveFileDialog sfd = new SaveFileDialog();
                        sfd.Filter = "Excel Files (.xls)|*.xls|All Files (*.*)|*.*";
                        sfd.FilterIndex = 1;
                        if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            string errorMessage = string.Empty;
                            if (((List<GestionePolveriLib.BL.Reports.ReportDirezionale>)result).ToExcel(sfd.FileName, out errorMessage))
                            {
                                MessageBox.Show("Esportazione in Excel effettuata!", "Gestione Polveri", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            else
                            {
                                MessageBox.Show(string.Format("ATTENZIONE: Si è verificato un errore: {0}", errorMessage), "Gestione Polveri", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Nessun dato da inviare in stampa.", "Gestione Polveri", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                });

            }
        }

        void cf2_ExecuteFilter(object sender, EventArgs e)
        {
            ManageData(m_filter.FilterBag);
        }

        protected override void OnLoad(EventArgs e)
        {
            ManageData(m_filter.FilterBag);

            base.OnLoad(e);
        }

        private void ManageData(ReportDirezionaleFilterBag filter)
        {
            try
            {
                this.ReportViewer.ZoomPercent = 130;

                this.ReportParameters.Clear();

                if(filter.UseDates)
                {
                    string periodo = string.Format("periodo {0} - {1}", filter.PeriodFrom.Value.ToShortDateString(), filter.PeriodTo.Value.ToShortDateString());
                    this.ReportParameters.Add(new ReportParameter("PeriodoReport", periodo));
                }
                else
                {
                    this.ReportParameters.Add(new ReportParameter("PeriodoReport", DateTime.Now.ToShortDateString()));
                }
                
                ReportDirezionaleFacade rdf = new ReportDirezionaleFacade(filter);
                rdf.RunAsync(() => rdf.GetReportDataDettaglioXXXX(), (result) =>
                {
                    SetUpReport(result, "ReportDirezionalePolveriXXXX");
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            //List<GestionePolveriLib.BL.Reports.ReportDirezionale> data = rdf.GetReportData();
            //SetUpReport(data, "ReportDirezionale2");
        }
    }

    public class ReportDirezionaleDettaglioPolveriXXXXBase : ReportManager2<ReportDirezionaleFilterBag> { }
}
